package cn.tansanqinger.controller.account;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;

import cn.tansanqinger.pojo.User;
import cn.tansanqinger.pojo.User1;
import cn.tansanqinger.service.account.IUserAccountService;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

/**
 * 用于对账号管理的数据处理与页面展示
 * @author C3H2
 * @time 2021.01
 */
@Controller
@RequestMapping("/admin/account")
public class AccountStudentController {
	
	@Autowired
	private IUserAccountService service;
//	@Autowired
//	private IOperationService opService;
	
	@RequestMapping("/list3")
	public ModelAndView list3(ModelAndView mv) {
		mv.setViewName("account/list3");
		return mv;
	}
	
	
	/**
	 * 对学生账号进行过滤，让学生账号的所有首页初始数据都可以从这里查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectCondition3")
	public @ResponseBody String selectCondition(HttpServletRequest request) {
		PageData pd = new PageData(request);
		try {
			String keywords = pd.getString("keywords");	//关键词检索条件
			if(null != keywords && !"".equals(keywords)){
				pd.put("keywords", keywords.trim());
			}
			pd = service.selectScreenAccount(pd,0);
			pd.put("code","0");
			pd.put("msg", "查询成功");
			return new ObjectMapper().writeValueAsString(pd);
		} catch (Exception e) {}
		return null;
	}
	
	/**
	 * 对学生账号管理list3编辑进行操作
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/list3_edit")
	public @ResponseBody ModelAndView list3_edit(HttpServletRequest request, ModelAndView mv) {
		PageData pd = new PageData(request);
		//对数据进行填充，然后给表格加以修改的功能
		mv.addObject("user", service.selectId(pd));
		mv.setViewName("account/list3_edit");
		return mv;
	}
	
	/**
	 * 学生账号管理详情进行操作
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/list3_details")
	public @ResponseBody ModelAndView list3_details(HttpServletRequest request, ModelAndView mv) {
		PageData pageData = new PageData(request);
		//对数据进行查询，所有信息不能进行修改，
		//查询报告对账号信息本身进行查询，还有操作账号的数据进行查询
		mv.addObject("user", service.selectId(pageData));
		//
		mv.setViewName("account/list3_details");
		return mv;
	}
	
	/**
	 * 对学生账号进行封号或者解封账号
	 * @param request
	 * @return
	 */
	@RequestMapping("/list3_del")
	public @ResponseBody PageData list3_del(HttpServletRequest request) {
		//PageData pageData = new PageData(request);
		
		return null;
	}
	
	@RequestMapping("/export3")
	public void export3(HttpServletRequest request, HttpServletResponse response) {
		try {
			PageData pd = new PageData(request);
			@SuppressWarnings("unchecked")
			List<User> user = (List<User>) service.selectScreenAccount(pd,0).get("data");
			List<User1> user1s = new ArrayList<User1>();
			User1 user1 = null;
			if(user!=null) {
				for (int i = 0; i < user.size(); i++) {
					user1 = new User1();
					user1.setXh(i+1+"");
					user1.setEmail(Tools.isEmpty(user.get(i).getEmail()));
					user1.setAccount(Tools.isEmpty(user.get(i).getAccount()));
					user1.setImg(Tools.isEmpty(user.get(i).getImg()));
					user1.setPhone(Tools.isEmpty(user.get(i).getPhone()));
					user1.setName(Tools.isEmpty(user.get(i).getName()));
					if(user.get(i).getSex()==0) {
						user1.setSex("保密");
					} else if(user.get(i).getSex()==1) {
						user1.setSex("男");
					} else if(user.get(i).getSex()==2) {
						user1.setSex("女");
					}
					user1.setAge(Tools.isEmpty(user.get(i).getAge()));
					if(user.get(i).getGrade()==0) {
						user1.setGrade("未知");
					} else if(user.get(i).getGrade()==1) {
						user1.setGrade("初三以下");
					} else if(user.get(i).getGrade()==2) {
						user1.setGrade("初三");
					} else if(user.get(i).getGrade()==3) {
						user1.setGrade("高一");
					} else if(user.get(i).getGrade()==4) {
						user1.setGrade("高二");
					} else if(user.get(i).getGrade()==5) {
						user1.setGrade("高三");
					} else if(user.get(i).getGrade()==6) {
						user1.setGrade("高三以上");
					}
					user1.setSchool(Tools.isEmpty(user.get(i).getSchool()));
					user1.setBrithday(Tools.isEmpty(user.get(i).getBrithday()));
					user1.setPersignature(Tools.isEmpty(user.get(i).getPersignature()));
					user1.setCreatetime(Tools.isEmpty(user.get(i).getCreatetime()));
					if(user.get(i).getJurisdiction()==0) {
						user1.setJurisdiction("学生");
					} else if(user.get(i).getJurisdiction()==1) {
						user1.setJurisdiction("教师");
					}
					if(user.get(i).getIsreport()==0) {
						user1.setIsreport("否");
					} else if(user.get(i).getIsreport()==1) {
						user1.setIsreport("否");
					} 
					if(user.get(i).getIsbanned()==0) {
						user1.setIsbanned("否");
					} else if(user.get(i).getIsbanned()==1) {
						user1.setSex("是");
					}
				}
			}
            //2、设置下载信息
			response.setContentType("application/vnd.ms-excel"); //下载excel
			response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("学生用户信息表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //3、调用EasyExcel的工具类完成生成下载
            EasyExcel.write(response.getOutputStream())
                    .head(User1.class) //设置表头
                    .sheet("heima127") //指定页名称
                    .doWrite(user1s); //设置数据
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
}
